<%@ page language="java" contentType="text/html; charset=UTF-8"  isErrorPage="true"
    pageEncoding="UTF-8"%>
<div class="layuimini-main">

    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">用户名</label>
            <div class="layui-input-block">
                <input type="hidden" name="id" value="">
                <input type="text" name="username" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">密码</label>
            <div class="layui-input-block">
                <input type="text" name="password" lay-verify="required" lay-reqtext="密码不能为空" placeholder="请输入密码" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="upload">
                    <i class="layui-icon">&#xe67c;</i>上传图片
                    <input type="hidden" value="" id="avatar"  name="avatar">
                </button>
                <br><br>
                <div style="width:200px;height:200px;border:3px solid #0099CC;border-radius: 5px;padding: 3px;">
                    <img style="max-width: 200px;max-height:200px;" id="preview">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">注册时间</label>
            <div class="layui-input-block">
                <input type="text" id="date" name="createdate" lay-verify="required" lay-reqtext="注册时间不能为空" placeholder="请输入注册时间" value="" class="layui-input">
            </div>
        </div>
        <%--<div class="layui-form-item">
              <label class="layui-form-label required">用户类型</label>
              <div class="layui-input-block">
                  <select name="type" lay-filter="role">
                      <option value="">请选择用户类型</option>
                      <option value="管理员">管理员</option>
                      <option value="家长">家长</option>
                      <option value="学生">学生</option>
                  </select>
              </div>
         </div>--%>
        
       <div class="layui-form-item layui-form-text">
           <label class="layui-form-label">备注信息</label>
           <div class="layui-input-block">
               <textarea name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>
           </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'table', 'laydate', 'upload'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            laydate = layui.laydate,
            upload = layui.upload,
            $ = layui.$;

        /*上传头像的开始  datetime yyyy-MM-dd HH：mm:SS和结束*/
        laydate.render({
            elem:'#date',
            type:'date',// datetime yyyy-MM-dd
            trigger:'click'
        })

        // 上传首页图片
        //$("#preview").attr("src",parent.images);
        //console.log(parent.blogType);
        upload.render({
            elem: '#upload',
            url: '/uploads',
            auto: true,//选择文件后不自动上传
            bindAction: '#commit',
            before: function () {//上传前的回调
                this.data = {name: $('input[name="name"]').val()}
            },
            choose: function (obj) {//选择文件后的回调
                obj.preview(function (index, file, result) {
                    $('#preview').attr('src', result);
                })
            },
            done: function (res, index, upload) { //操作成功的回调
                var code = res.code === 0 ? 1 : 2;
                var images = document.getElementById("avatar");
                images.value=res.src;
            },
            error: function (index, upload) { //上传错误回调
                layer.alert('上传失败！' + index);
            }
        });
        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.alert("确定添加用户信息！", {
                title: '提交信息'
            }, function () {
            	/* console.log(data); */
                $.ajax({
               	 url:"/user/addUser",
               	 data:JSON.stringify(data.field),
               	 type:"POST",
               	 dataType:"json",
                 contentType:"application/json",
               	 success:function(data){
               		 console.log(data);
               		 // data = JSON.parse(data);
               		 if(data.code == "0000") {
               			// 关闭弹出层
                        layer.close(index);
                        layer.close(parentIndex);
                        $(".layui-laypage-btn")[0].click();
               		 }else{
                         layer.alert('添加失败！请输入正确信息。'
                             +(data.msg!=undefind?data.msg:""))
                 	}
               	 }
                })
            });
            return false;
        });
    });
</script>